﻿package com.vicentediaz.view.intro
{
    import com.greensock.*;
    import com.greensock.easing.*;
    import com.greensock.plugins.*;
    import com.vicentediaz.view.events.*;
    import com.vicentediaz.view.imageviewer.*;
    import flash.events.*;
    import flash.filters.*;
    import org.papervision3d.materials.*;
    import org.papervision3d.objects.*;
    import org.papervision3d.objects.primitives.*;
    import org.papervision3d.view.*;

    public class Intro3D extends BasicView
    {
        private var shadow:ShadowLightAsset;
        private var container:DisplayObject3D;
        private var shadowContainer:DisplayObject3D;
        private var logo:LogoAsset;
        private var glow:GlowFilter;
        private var logoPlane:Plane;
        private var logoMaterial:MovieMaterial;
        private var shadowPlane:Plane;
        private var shadowMaterial:MovieMaterial;

        public function Intro3D(param1:Number = 640, param2:Number = 480, param3:Boolean = true, param4:Boolean = false, param5:String = "Target")
        {
            super(param1, param2, param3, param4, param5);
            this.container = new DisplayObject3D();
            this.logo = new LogoAsset();
            this.logoMaterial = new MovieMaterial(this.logo, true, true);
            this.logoPlane = new Plane(this.logoMaterial, this.logo.width, this.logo.height, 3, 3);
            this.logoMaterial.smooth = true;
            this.shadow = new ShadowLightAsset();
            this.shadowMaterial = new MovieMaterial(this.shadow, true, true, true);
            this.shadowPlane = new Plane(this.shadowMaterial, this.shadow.width, this.shadow.height, 8, 8);
            this.shadowMaterial.smooth = true;
            this.shadowContainer = new DisplayObject3D();
            this.logoPlane.y = this.logo.height * 0.5;
            this.shadowPlane.y = (-this.shadow.height) * 0.5 + 2;
            this.shadowPlane.x = -1;
            this.shadowContainer.rotationX = 90;
            camera.z = -1000;
            camera.zoom = 100;
            this.shadowContainer.addChild(this.shadowPlane);
            this.container.addChild(this.logoPlane);
            this.container.addChild(this.shadowContainer);
            this.container.rotationX = -90;
            this.container.scale = 1;
            scene.addChild(this.container);
            addChild(viewport);
            this.startAnimation();
            addEventListener(Event.ENTER_FRAME, this.tick, false, 0, true);
            ImageViewer.getInstance().addEventListener(ImageViewerEvent.IMAGE_LOADED, this.onFirstImageLoaded, false, 0, true);
            return;
        }// end function

        public function startAnimation() : void
        {
            TweenPlugin.activate([GlowFilterPlugin]);
            var _loc_1:Array;
            this.glow = new GlowFilter(16777215, 1, 10, 10, 2, 2);
            this.filters = [this.glow];
            var _loc_2:uint;
            while (_loc_2++ < _loc_1.length)
            {
                // label
                TweenMax.allFrom([this.logo[_loc_1[_loc_2]], this.shadow[_loc_1[_loc_2]]], Math.random() * 3 + 4, {alpha:0, delay:Math.random() * 4, ease:Quad.easeOut});
            }// end while
            TweenMax.to(this.container, 7, {rotationX:0, rotationY:0, rotationZ:0, delay:0, ease:Quad.easeInOut, onComplete:this.end});
            TweenMax.to(this, 7, {glowFilter:{alpha:0}, delay:3, onComplete:this.stopRender});
            return;
        }// end function

        private function stopRender() : void
        {
            removeEventListener(Event.ENTER_FRAME, this.tick);
            return;
        }// end function

        private function onSecondImageLoaded(param1:ImageViewerEvent) : void
        {
            TweenMax.to(this, 1, {alpha:0, visible:false});
            return;
        }// end function

        private function end() : void
        {
            dispatchEvent(new IntroEvents(IntroEvents.FINISH));
            return;
        }// end function

        private function tick(param1:Event) : void
        {
            singleRender();
            return;
        }// end function

        private function onFirstImageLoaded(param1:ImageViewerEvent) : void
        {
            ImageViewer.getInstance().removeEventListener(ImageViewerEvent.IMAGE_LOADED, this.onFirstImageLoaded);
            ImageViewer.getInstance().addEventListener(ImageViewerEvent.IMAGE_LOADED, this.onSecondImageLoaded, false, 0, true);
            return;
        }// end function

    }
}
